<!--
 * @Date: 2021-10-27 09:15:50
 * @LastEditors: 邓春宜
 * @Desc: 单曲线样式1，没有坐标轴，平滑曲线
 * @LastEditTime: 2021-11-24 09:25:01
 * @FilePath: \tianjushi-kanban\src\components\charts\SingleBar2.vue
-->
<template>
  <div :style="[{ width: width, height: height, overflow: 'hidden' }]">
    <div
      ref="echartref"
      :style="[{ width: width, height: height }, _resetChartStyle]"
    ></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

import { onMounted, toRefs, ref, reactive, watch, computed } from "vue";
import { resetChartStyle, showLoading } from "@/core/tools/common";
export default {
  props: {
    title: {
      type: String,
      default: ":title='设置标题'",
    },
	yname: {
      type: String,
      default: "",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    chartdata: {
      type: Array,
      default: {
        xData: ["1月", "2月", "3月", "4月", "5月"],
        yData: [39.1, 93.2, 97.3, 26.3, 26.3],
      },
    },
    color: {
      type: Array,
      default: ["#00feff", "#027eff", "#0286ff"],
    },
    fontSize: {
      default: 16,
      type: Number,
    },
    max: {
      default: 100,
      type: Number,
    },
  },
  setup: function (props, context) {
    /*监听props*/
    watch(props, (newProps, oldProps) => {
      f_echartInit(state.chartInit);
    });

    let state = reactive({
      echartref: ref(),
      chartInit: "",
    });

    const _resetChartStyle = computed({
      get() {
        return resetChartStyle();
      },
    });

    const f_echartInit = (chartdom) => {
      const option = {
        tooltip: {},
        animation: false,
        grid: {
          top: "10%",
          bottom: "20%", //也可设置left和right设置距离来控制图表的大小
        },
        xAxis: {
          data: props.chartdata.xData,
          axisLine: {
            show: true, //隐藏X轴轴线
            lineStyle: {
              color: "#11417a",
            },
          },
          axisTick: {
            show: false, //隐藏X轴刻度
          },
          axisLabel: {
            show: true,
            margin: 14,
            fontSize: props.fontSize,
            textStyle: {
              color: "#A3C0DF", //X轴文字颜色
            },
          },
        },
        yAxis: [
          {
            type: "value",
            name: props.yname,
            nameTextStyle: {
              color: "#fff",
              fontSize: props.fontSize,
            },
            gridIndex: 0,
            min: 0,
            max: props.max,
           // interval: 25,
             splitNumber: 6,
            splitLine: {
              show: true,
              lineStyle: {
                color: "#113763",
                width: 1,
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#11417a",
              },
            },
            axisLabel: {
              show: true,
              margin: 14,
              fontSize: props.fontSize,
              textStyle: {
                color: "#A3C0DF",
              },
            },
          },
        ],
        series: [
          {
            name: "",
            type: "bar",
            barWidth: 30,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: props.color[0],
                  },
                  {
                    offset: 1,
                    color: props.color[1],
                  },
                ]),
              },
            },
            data: props.chartdata.yData,
            z: 10,
            zlevel: 0,
            label: {
              show: true,
              position: "top",
              distance: 10,
              fontSize: props.fontSize,
              color: props.color[0],
            },
          },
          {
            // 分隔
            type: "pictorialBar",
            itemStyle: {
              normal: {
                color: "#0F375F",
              },
            },
            symbolRepeat: "fixed",
            symbolMargin: 10,
            symbol: "rect",
            symbolClip: true,
            symbolSize: [30, 2],
            symbolPosition: "start",
            symbolOffset: [0, -1],
            // symbolBoundingData: this.total,
            data: props.chartdata.yData,
            width: 40,
            z: 0,
            zlevel: 1,
          },
        ],
      };

      chartdom.setOption(option);
      chartdom.hideLoading();
    };
    //挂载
    onMounted(() => {
      state.chartInit = echarts.init(state.echartref);
      state.chartInit.showLoading(showLoading());
      f_echartInit(state.chartInit);
    });
    return {
      ...toRefs(state),
      _resetChartStyle,
    };
  },
};
</script>

<style>
</style>